package com.cqwshzj.framework.service.impl;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/7 14:26
 */

import com.cqwshzj.common.JobConstant;
import com.cqwshzj.framework.dao.api.JobErrRptDao;
import com.cqwshzj.framework.dao.api.JobInfoDao;
import com.cqwshzj.framework.dao.model.JobErrRpt;
import com.cqwshzj.framework.dao.model.JobInfo;
import com.cqwshzj.framework.service.JobErrRptService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/7
 * @since 1.0.0
 */
@Service
public class JobErrRptServiceImpl implements JobErrRptService {

	public static final Logger log = LoggerFactory.getLogger(JobErrRptServiceImpl.class);

	@Autowired
	private JobErrRptDao dao;
	@Autowired
	private JobInfoDao jobInfoDao;

	@Override
	public int addJobErrRpt(String jobId, String jobState, String message, String exception, Date occurTime) {
		Assert.hasText( jobId,"缺少参数 jobId");
		Assert.hasText( jobState,"缺少参数 jobState");
		Assert.notNull( occurTime,"缺少参数 occurTime");
//		Assert.hasText( message,"缺少参数 message");
//		Assert.hasText( exception,"缺少参数 exception");

		JobInfo jobInfo = jobInfoDao.selectById(jobId);
		Assert.notNull( jobInfo ,"缺少人物信息的查询结果 jobId="+jobId );

		String occurDateStr = DateFormatUtils.format(occurTime, JobConstant.DATE_1 );
		String crtTimeStr = DateFormatUtils.format( new Date(), JobConstant.DATETIME_1 );

		JobErrRpt entity = new JobErrRpt();
		entity.setOccurdate( occurDateStr );
		entity.setJobId( jobId );
		entity.setStrtId( jobInfo.getStrtId() );
		entity.setJobType( jobInfo.getJobType() );
		if (StringUtils.isNotEmpty( jobState )) {
			entity.setJobState( jobState );
		}else{
			entity.setJobState( jobInfo.getState() );
		}

		if (StringUtils.isNotEmpty( exception )) {
			int maxLength = 400;
			if ( exception.length() < maxLength ){
				entity.setException( exception );
			}else{
				entity.setException( exception.substring(0,maxLength) );
			}
		}

		if (StringUtils.isNotEmpty(message)) {
			int maxLength = 120;
			if (message.length() < maxLength) {
				entity.setMessage( message );
			} else {
				entity.setMessage( message.substring(0,maxLength) );
			}
		}
		entity.setCrtTime( crtTimeStr );
		entity.setStatus( JobConstant.STATUS_ENABLE );
		return dao.insert(entity);
	}
}
